// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_render_input.h"
#include "mk_error.h"
#include "mk_engine.h"

namespace MK3DE
{
	CRenderInput::CRenderInput() : _indexBuffer(NULL), _elemNum(0)
	{
	}

	CRenderInput::~CRenderInput()
	{
		SAFE_DELETE_ARRAY(_renderInputElemDesc);
	}

	BOOL CRenderInput::_Create(RenderInputElemDesc* desc, DWORD size)
	{
		BOOL ret = TRUE;
		_renderInputElemDesc = new RenderInputElemDesc[size];
		MK_PROCESS_ERROR(_renderInputElemDesc);

		memcpy(_renderInputElemDesc, desc, sizeof(RenderInputElemDesc) * size);

		for (DWORD i = 0; i < size; ++i)
			_vertexStream.push_back(NULL);

		_elemNum = size;
		ret = TRUE;
Exit0:
		return ret;
	}

	CHardwareBuffer* CRenderInput::GetVertexBuffer(DWORD idx)
	{
		if (idx < _vertexStream.size())
			return _vertexStream[idx];

		return NULL;
	}

	CHardwareBuffer* CRenderInput::GetIndexBuffer()
	{
		return _indexBuffer;
	}

	BOOL CRenderInput::SetVertexBuffer(DWORD idx, CHardwareBuffer* vb)
	{
		if (idx < _vertexStream.size())
		{
			_vertexStream[idx] = vb;
			return TRUE;
		}

		return FALSE;
	}

	void CRenderInput::SetIndexBuffer(CHardwareBuffer* ib)
	{
		ASSERT(ib);
		_indexBuffer = ib;
	}

	BOOL CRenderInput::GetElementDesc(DWORD idx, RenderInputElemDesc& desc)
	{
		if (idx < _elemNum)
		{
			desc = _renderInputElemDesc[idx];
			return TRUE;
		}

		return FALSE;
	}
}

